<?php 
class Country_Widget_ListMemberController extends Engine_Content_Widget_Abstract{
   public function indexAction(){
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return $this->setNoRender();
    }

    // Get subject and check auth
    $this->view->subject = $subject = Engine_Api::_()->core()->getSubject('country');	

     $request = Zend_Controller_Front::getInstance()->getRequest();
     
     $managerTable = Engine_Api::_()->getDbtable('managers','country');
     $this->view->isManager = $managerTable->isManager($subject->country_id,$viewer->user_id);
     
     $this->view->listManager = $managerTable->getUserManagerId($subject->country_id);
        
        
     $countrymemberTb = Engine_Api::_()->getDbTable('countrymembers','country');
     $countrymemberTbName = $countrymemberTb->info('name');
     
     $userTb = Engine_Api::_()->getDbTable('users','user');
     $userTName = $userTb->info('name');
     
     $select = $userTb->select()
                      ->from($userTName)  
                      ->setIntegrityCheck(false)
                      ->join($countrymemberTbName,$userTName.".user_id = {$countrymemberTbName}.user_id",null)
                      ->where('country_id = ?', $subject->getIdentity())
                      ->where("user_approved = 1")
                      ->where('country_approved = 1');
    
    $username = '';                    
    if (isset($_POST['username']))  {
        $username = $_POST['username'];
    } 
    
    if(!empty($username)){
        $select = $select->where(" email = '{$username}' or displayname like '%{$username}%' ");
    }
    
    $this->view->active_div = true;
    
    $this->view->search = false; 
    if ( isset($_POST['search_member']) )  {
        $this->view->search = true;
    }  

     $this->view->paginator = $paginator = Zend_Paginator::factory($select);
     $paginator->setItemCountPerPage(20);
     $page = $request->getParam('page',1);
     $paginator->setCurrentPageNumber($page);
     
   } 
}
?>